package com.springboot.admin.controller;

import com.springboot.admin.common.ResponseMap;
import com.springboot.admin.entity.CateDto;
import com.springboot.admin.entity.CommentsDto;
import com.springboot.admin.entity.PagesDto;
import com.springboot.admin.mail.MailSenderBoot;
import com.springboot.admin.service.AccountService;
import com.springboot.admin.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Api(tags = "登录后相关接口")
public class AdminController {

  @Autowired
  MailSenderBoot mailSenderBoot;
  @Autowired
  AccountService accountService;
  @Autowired
  AdminService adminService;

  @GetMapping("/getDetail/{id}")
  @ApiOperation(value="查询详情接口")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap getOne(@PathVariable("id") String id) {
    return ResponseMap.success(adminService.getDetail(id));
  }

  @PostMapping("/getList")
  @ApiOperation(value="查询列表接口")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap getList(@RequestBody PagesDto PagesDto) {
    return ResponseMap.success(adminService.getListByCatePage(PagesDto));
  }

  @ApiOperation(value="查询所有分类")
  @GetMapping("/getCate")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getCate() {
    return ResponseMap.success(adminService.getCateList());
  }

  @ApiOperation(value="恢复分类")
  @PostMapping("/revertCate")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap revertCate(@RequestBody CateDto cateDto) {
    adminService.revertCate(cateDto);
    return ResponseMap.success();
  }

  @PostMapping("/insertItem")
  @ApiOperation(value="新增文章接口")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap insertItem(@RequestBody PagesDto pagesDto) {
    adminService.insertOne(pagesDto);
    return ResponseMap.success();
  }

  @PostMapping("/deleteItem")
  @ApiOperation(value="删除文章接口")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap delOne(@RequestBody PagesDto PagesDto) {
    adminService.delOne(PagesDto.getId());
    return ResponseMap.success();
  }

  @PostMapping("/updateItem")
  @ApiOperation(value="更新文章接口")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap updateOne(@RequestBody PagesDto pagesDto) throws Exception {
    adminService.updateOne(pagesDto);
    return ResponseMap.success();
  }

  @PostMapping("/insertCate")
  @ApiOperation(value="新增分类接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap inertOne(@RequestBody CateDto cateDto) {
    adminService.insertCate(cateDto);
    return ResponseMap.success();
  }

  @PostMapping("/updateCate")
  @ApiOperation(value="更新分类接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateCate(@RequestBody CateDto cateDto) {
    adminService.updateCate(cateDto);
    return ResponseMap.success();
  }

  @PostMapping("/delCate")
  @ApiOperation(value="删除分类接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delCate(@RequestBody CateDto cateDto) {
    adminService.delCate(cateDto.getId());
    return ResponseMap.success();
  }

  @GetMapping("/getSourceList")
  @ApiOperation(value="获取资源接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getImageList() {
    Map siteInfo = accountService.getSiteInfo();
    String realPath = (String) siteInfo.get("sourceRealUrl");
    //  String realPath = "/Users/wilkwo/Downloads/workspace/test";
    //  File dir =new File("/data/wwwroot/default/uploadFile");//浏览F盘a文件夹下的所有内容
    File dir =new File(realPath);//浏览F盘a文件夹下的所有内容
    // File dir =new File("/Users/xxn/Downloads/");//浏览F盘a文件夹下的所有内容
    File[] files=dir.listFiles();   //列出所有的子文件
    ArrayList arrayList = new ArrayList();
    for(File file :files) {
      if(file.isFile()){
        //如果是文件，则输出文件名字
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("filename", file.getName());
        arrayList.add(itemMap);
      }else if(file.isDirectory()) {
        //如果是文件夹，则输出文件夹的名字，并递归遍历该文件夹
//        System.out.println(file.getName());
      }
    }
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", arrayList);
    resultMap.put("sourceUrl", (String)siteInfo.get("sourceUrl"));
    return new ResponseMap(0, "更新成功", resultMap);
  }

  @PostMapping("/delFile")
  @ApiOperation(value="删除文件接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delFile(@RequestBody Map fileInfo) {
    Map siteInfo = accountService.getSiteInfo();
    String realPath = (String) siteInfo.get("sourceRealUrl");
    //  String realPath = "/Users/wilkwo/Downloads/workspace/test/";
    File file = new File(realPath + fileInfo.get("filename"));
    if (file.exists()) {
      file.delete();
      return new ResponseMap(0, "更新成功", file);
    }
    return new ResponseMap(1, "更新失败", file);
  }

  @PostMapping("/getInsertComment")
  @ApiOperation(value="添加评论接口")
  //@PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap insertComment(@RequestBody CommentsDto commentsDto) {
    adminService.insertComment(commentsDto);
    return ResponseMap.success();
  }

  @PostMapping("/delComment")
  @ApiOperation(value="删除评论接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delComment(@RequestBody CommentsDto commentsDto) {
    adminService.delComment(commentsDto.getId());
    return ResponseMap.success();
  }

  @PostMapping("/mail")
  @ApiOperation(value="发送邮件接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap sendMail(@RequestBody Map map) {
    mailSenderBoot.sendMail((String) map.get("mailTo") ,(String)map.get("content"), "邮件测试");
    return ResponseMap.success();
  }

  @PostMapping("/getUserList/{page}")
  @PreAuthorize("hasRole('ROLE_admin')")
  @ApiOperation(value="查询用户列表接口")
  public ResponseMap getUserList(@PathVariable("page") String page) {
    return ResponseMap.success(adminService.getUserListByPage(page));
  }

  @PostMapping("/getRoleList")
  @ApiOperation(value="查询用户角色列表接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getRoleList() {
    return ResponseMap.success(adminService.getRoleList());
  }

  @PostMapping("/updateRole")
  @ApiOperation(value="更新用户角色接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateRole(@RequestBody Map<String,Object> roleMap) {
    adminService.updateRole(roleMap);
    return ResponseMap.success();
  }

  @PostMapping("/insertRole")
  @ApiOperation(value="新增用户角色接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap insertRole(@RequestBody Map map) {
    adminService.insertRole(map);
    return ResponseMap.success();
  }

  @PostMapping("/deleUser")
  @ApiOperation(value="删除用户接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap deleUser(@RequestBody Map map) {
    accountService.deleUser( (int)map.get("id"), (String) map.get("username"));
    return ResponseMap.success();
  }

  @PostMapping("/getSiteInfo")
  @ApiOperation(value="获取站点信息接口")
  //  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getSiteInfo() {
    return ResponseMap.success(accountService.getSiteInfo());
  }

  @PostMapping("/generateKeywords")
  @ApiOperation(value="生成关键字")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap generateKeywords() {
    adminService.generateKeywords();
    return ResponseMap.success();
  }

  @PostMapping("/clearCache")
  @ApiOperation(value="redis缓存")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap clearCache() {
    adminService.clearCache();
    return ResponseMap.success();
  }

}
